package com.business.controller;

import com.business.Service.TestService;
import com.business.api.TestApi;
import com.business.async.TestAsync;
import com.comm.result.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


/**
 * @Description: TODO
 * @Author: mis_wu
 * @Date: 2022/6/30
 **/
@RestController
@RequestMapping("/activity")
@Slf4j
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private TestAsync testAsync;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private TestApi testApi;

    @Value("${server.port}")
    private String port;

    @Value("${spring.application.name}")
    private String applicationName;

    @GetMapping("/activityTest")
    public String test(String name) {
        log.info("feign activity服务:入参:{},端口:{},服务:{}",name,port,applicationName);
        //ResponseEntity<String> result = restTemplate.getForEntity("http://localhost:10010/cloud-server/test/testFeign?name="+name,String.class);
        ResponseResult<Object> result = testApi.testFeign(name);
        String test = testService.test(name);
        testAsync.testAsync();
        log.info("TestController test执行结束 ...");
        return "activity访问serve成功 ==> test返回:"+test+"==>"+result.getData();
    }

    @GetMapping("/test1")
    public String test1(String name) {
        log.info("test1执行:入参:{},端口:{},服务:{}",name,port,applicationName);
        return "test1访问成功 ==> test1返回:"+name;
    }


    @GetMapping("/testDruid")
    public String testDruid(String name) {
        String test = testService.test(name);
        return "testDruid访问成功 ==> 返回值:"+test;
    }

    /**
     * 测试多数据源 默认数据源 主库
     * @param param param
     * @return string
     */
    @GetMapping("/testDynamicMaster")
    public String testDynamic(String param){
        String result = testService.testDynamic(param);
        return "主库[默认数据源测试结果]:"+result;
    }

    /**
     * 测试多数据源 默认数据源 指定库
     * @param param param
     * @return string
     */
    @GetMapping("/testDynamicSlave1")
    public String testDynamicSlave1(String param){
        String result = testService.testDynamicSlave1(param);
        return "从库[slave数据源测试结果]:"+result;
    }

}
